using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
namespace com.dtn.deals
{
	public class tbl_BannerManager : Itbl_BannerManager
	{
		private static Itbl_BannerManager itbl_BannerManager = null;
		private tbl_BannerManager()
		{
		}
		public static Itbl_BannerManager CreateInstance()
		{
			if (itbl_BannerManager == null)
			{
				itbl_BannerManager = new tbl_BannerManager();
			}
			return itbl_BannerManager;
		}
		public int Inserttbl_Banner(tbl_Banner tbl_banner)
		{
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@name", SqlDbType.NVarChar),
																new SqlParameter("@position", SqlDbType.TinyInt),
																new SqlParameter("@imgUrl", SqlDbType.NVarChar),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = tbl_banner.name;
			parameters[2].Value = tbl_banner.position;
			parameters[3].Value = tbl_banner.imgUrl;
			parameters[4].Value = tbl_banner.cDate;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_Banner", CommandType.StoredProcedure, parameters);
				tbl_banner.ID = Convert.ToInt32(parameters[0].Value);
			}
			catch (Exception ex)
			{
				tbl_banner.ID = -1;
			}
			return tbl_banner.ID;
		}
		public bool Updatetbl_Banner(tbl_Banner tbl_banner)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@name", SqlDbType.NVarChar),
																new SqlParameter("@position", SqlDbType.TinyInt),
																new SqlParameter("@imgUrl", SqlDbType.NVarChar),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
			parameters[0].Value = tbl_banner.ID;
			parameters[1].Value = tbl_banner.name;
			parameters[2].Value = tbl_banner.position;
			parameters[3].Value = tbl_banner.imgUrl;
			parameters[4].Value = tbl_banner.cDate;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_Banner", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public bool Deletetbl_Banner(int id)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_Banner", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public tbl_Banner Selecttbl_BannerByID(int id)
		{
			IDataReader dr;
			tbl_Banner tbl_banner = new tbl_Banner();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				dr = dataProvider.ExecuteReader("sp_Selecttbl_Banner", CommandType.StoredProcedure, parameters);
				while(dr.Read())
				{
					tbl_banner.ID = Convert.ToInt32(dr["ID"].ToString());
					tbl_banner.name = (dr["name"].ToString());
                    tbl_banner.position = Convert.ToInt32(dr["position"].ToString());
					tbl_banner.imgUrl = (dr["imgUrl"].ToString());
					tbl_banner.cDate = Convert.ToDateTime(dr["cDate"].ToString());
				}

			}
			catch (Exception ex)
			{
				return null;
			}
			return tbl_banner;
		}
		public DataTable Selecttbl_BannersDynamic(string Where, string Order)
		{
			DataSet ds = new DataSet();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
			parameters[0].Value = Where;
			parameters[1].Value = Order;
			try
			{
				ds = dataProvider.ExecuteDataSet("sp_Selecttbl_BannersDynamic", CommandType.StoredProcedure, parameters);
			}
			catch (Exception ex)
			{
				return null;
			}
			return ds.Tables["Table"];
		}
	}
}
